<?php

namespace App\Models\Shop;

use App\Helpers\SelfEncrypt;
use App\Models\BaseModel;
use Carbon\Carbon;
use DC\UserCenter\Models\UserWalletModel;
use App\Models\User\AccountFlowModel;
use DC\BonusCenter\Services\ShoppingBonus;
use Illuminate\Support\Facades\DB;
use App\Models\Shop\UpgradeModel;
use Frontend\Models\UserBaseModel;

class UpgradeInfoModel extends BaseModel
{
    protected $table = 'upgrade_info';

    protected $primaryKey = 'id';

    public $dateFormat = 'U';

    public $dates = [
        'created_at',
        'updated_at',
        'paid_at'
    ];


    public function ApplyUser()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','user_id','member_id');
    }

    public function HandUser()
    {
        return $this->belongsTo('DC\UserCenter\Models\UserBaseModel','hand_uid','member_id');
    }

    public function ApplyUserInfo()
    {
        return $this->hasOne('DC\UserCenter\Models\UserInfoModel','member_id','user_id');
    }

    public function HandUserInfo()
    {
        return $this->hasOne('DC\UserCenter\Models\UserInfoModel','member_id','hand_uid');
    }


    public function createUpgradeInfo($upgrade_info)
    {
        $pay_price = abs(get_rule('gw_award1'));
        $now = Carbon::now()->timestamp;
        $model = new  self();
        $model->agent_sn = SelfEncrypt::createSn('XF',14);
        $model->user_id = $upgrade_info['user_id'];
        $model->hand_uid = $upgrade_info['hand_uid'];
        $model->up_before_level = $upgrade_info['up_before_level'];
        $model->up_after_level = $upgrade_info['up_after_level'];
        $model->pay_money = $pay_price;
        $model->status = 0;
        $model->created_at = $now;
        $model->upgrade_id=$upgrade_info['upgrade_id'];
        if($model->save()){
            return $model->id;
        }
        return false;
    }


    public function payOrder($order_id,$user_id,$voucher,$remark)
    {
        $order = self::where('user_id',$user_id)->where('status',0)->find($order_id);
        if(!$order){
            $this->errorMsg ='订单不存在或已支付';
            return false;
        }

         $res= self::where('id',$order_id)->update([
                'voucher'=>$voucher,
                'remark'=>$remark,
                'status'=>1,
                'paid_at'=>Carbon::now()->timestamp
         ]);

         if($res){
              return true;
          }
        return false;
    }

    public function handleOrder($order_id,$user_id,$status)
    {
        $order = self::where('hand_uid',$user_id)->where('status',1)->find($order_id);

        if(!$order){
            $this->errorMsg ='订单不存在或已支付';
            return false;
        }

        $res= self::where('id',$order_id)->update([
            'status'=>$status,
            'updated_at'=>Carbon::now()->timestamp
        ]);

        if($res){

            $order_info = self::where('hand_uid',$user_id)->where('status',$status)->find($order_id);
            if($status == 2){

                $upgrade_info =UpgradeModel::init()->where('id',$order_info['upgrade_id'])->first();

                $count = self::where('upgrade_id',$upgrade_info['id'])->count();

                $confirm_count = self::where('upgrade_id',$upgrade_info['id'])->where('status',2)->count();

                if($count == $confirm_count){
                    $result = UpgradeModel::init()->where('id',$order_info['upgrade_id'])->update(['status'=>2,'updated_at'=>Carbon::now()->timestamp]);
                    if($result){
                        $now_level = UserBaseModel::init()->where('member_id',$upgrade_info['user_id'])->value('level');
                        if($now_level == 0){
                            UserBaseModel::init()->where('member_id',$upgrade_info['user_id'])->update(['is_active'=>1]);
                            self::UpdateTuiLevel($upgrade_info['user_id']);
                        }else{
                            UserBaseModel::init()->where('member_id',$upgrade_info['user_id'])->update(['level'=>$upgrade_info['up_after_level']]);
                        }

                    }
                }
                ShoppingBonus::make()->award($user_id,$order->pay_money);
                ShoppingBonus::make()->Outaward($user_id);
            }elseif($status == 3){
                UpgradeModel::init()->where('id',$order_info['upgrade_id'])->update(['status'=>3,'updated_at'=>Carbon::now()->timestamp]);
            }

            return true;
        }
        return false;
    }


    // 判断推荐人 是否能 升一级
    public function UpdateTuiLevel($uid){

        $userInfo =  UserBaseModel::init()->where('member_id',$uid)->where('status','>=',0)->where('is_active',1)->first();

        if($userInfo){
            if($userInfo['referrer_id'] >0){
                $t_info =  UserBaseModel::init()->where('member_id',$userInfo['referrer_id'])->where('level',0)->where('status','>=',0)->where('is_active',1)->first();
                if($t_info){
                    $active_count =  UserBaseModel::init()->where('referrer_id',$t_info['member_id'])->where('level',0)->where('status','>=',0)->where('is_active',1)->count();
                    if($active_count >= get_rule('tj_max_deep')){
                        UserBaseModel::init()->where('member_id',$t_info['member_id'])->update(['level'=>1]);
                    }
                }
            }
        }
        return false;
    }

}
